/*
Class Name      : MessagesController
Created by      : Suneetha Allam Venkata
Created On      : 12/02/2013
Description     : This class helps to Manage Message
                    1.CaseManager Can Create Messages
                    2.Every User Should View All
Ref             : Minutes of meeting  on 12/02/2013 
Version         : 1.0               
*/
/*
Class Name      : MessagesController
Updated by      : Sreekanth SV Vegi
Updated On      : 22/02/2013
Description     : This class helps to display one message,top 5 messages and all messages based on user request.
                    1.CaseManager Can Create Messages
Version         : 1.0               
*/

 
public with sharing class MessagesController {
    
    //Variablest
   public   List<Message__c> MessagesList{get; set;}  //Top Message to view on Vf Page
   
    public boolean MessageVisibilityFlag{get;set;}// Result Visible flag
    public boolean Next5MessagesVisibilityFlag{get;set;}
    public boolean LinkVisibilityFlag{get;set;}
    public boolean ViewAllLinkVisibilityFlag{get;set;}
    public String CreatedDate{get;set;}
    public boolean  activatePostMessage{get; set;}// IsUploadflg visible for CaseManger
    public Datetime dateforcomparing;
   // IsUploadflg visible for CaseManger
    Message__c Message ;
    
 public MessagesController (ConsoleHomeController_AC controller){
    
      Init();
        
        
 }
  public MessagesController (){
    Init();
  }
  
  
    void Init()
    {
        
        activatePostMessage=false;
        //activatePostMessage=true;
        LinkVisibilityFlag=true;
        ViewAllLinkVisibilityFlag=true;
        String ProfileName = [select id, name from profile where id=:UserInfo.getProfileId() LIMIT 1].name;
        If(ProfileName=='West Case Management - Case Manager' || ProfileName=='West - Administration'){
          activatePostMessage=true;
          
        }
        this.Message = new   Message__c();
        Message.Description__c=''; 
       MessageVisibilityFlag=false;
       MessagesList= [SELECT Name,Description__c,CreatedTime__c FROM Message__c   Order by  CreatedTime__c desc limit 1];
       //for(Message__c mess:MessagesList)
       //Datetime dt=mess.CreatedTime__c;
       //CreatedDate=dt.day();
       //DateTime dT = System.now();
       //Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());
       //Next5Messages= [SELECT Name,Description__c,CreatedTime__c FROM Message__c Order by CreatedTime__c  limit 5];
       //MoreMessages= [SELECT Name,Description__c,CreatedTime__c FROM Message__c Order by CreatedTime__c ];
       if(MessagesList.size()>0)
         MessageVisibilityFlag=true;
          
        
    }
    
    //Get Message
    public  Message__c getMessage(){
        
        return Message;
        
    }
    public PageReference Top5messages()
    {
      MessagesList=[SELECT Name,Description__c,CreatedTime__c FROM Message__c Order by CreatedTime__c desc limit 5];
      if(MessagesList.size()>0)
      {
         MessageVisibilityFlag=true;
         LinkVisibilityFlag=false;
      }
      return null;
    }
    public PageReference AllMessages()
    {
        MessagesList=[SELECT Name,Description__c,CreatedTime__c FROM Message__c Order by CreatedTime__c desc];
        if(MessagesList.size()>0)
        {
         MessageVisibilityFlag=true;
         LinkVisibilityFlag=false;
         ViewAllLinkVisibilityFlag=false;
         }
        return null;
    }
   
    //===============================================================//
    //========================== Top5Message================================//
    // Helps to Get Top 5 Messages        //
    //===============================================================//
  
  //==============================================================//
    //===Error Function:Helps to diplay error messages on VF page ===//
    //==============================================================//
  public void errMessage(string  ErrMsgStr)
   {
       ApexPages.Message myErrMsg=new ApexPages.Message(ApexPages.Severity.ERROR, ErrMsgStr );
       ApexPages.AddMessage(myErrMsg); 
    }
    
      
    
     //===============================================================//
    //========================== PostMessate================================//
    // Helps to PostMessage         //
    //===============================================================//
     public PageReference doPostMessage()
      {
      
      try
        {
      String ErrMsgForNotesStr='';
     
             if( Message.Description__c==null )
                        {
                           ErrMsgForNotesStr ='Please enter Message ' ;
                           
                        }
                       
             if(Message.Description__c.length()==0)
                            {
                             ErrMsgForNotesStr ='Please enter Message '; 
                           
                            }
                            
            if(ErrMsgForNotesStr!='')
                                {
                               // errMessage('Please enter Message ' );
                                 return null;

                                }
        insert  Message;
         Init();
                 
        }
        catch(Exception e)
        {
           //  errMessage('Exceptiion in task==:' + e.getMessage());
           //  system.debug('Exceptiion ==:'+e);   
           //  GeneralUtilWCM.saveLog( 'Sorry you don\'t have permissions to close the cases', ' ConfirmAndCloseCaseForWCM',' ConfirmAndCloseCaseForWCM');
        }   
           return null;

      }
     
   
}